/*
 * Copyright (c) Modify by T C L on 2019.3.1.
 */

package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.egg.ylt.R;
import com.egg.ylt.Utils.StringSpanUtils;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.controls.DIALOG_CouponTip;
import com.egg.ylt.pojo.CouponEntity;
import com.yonyou.framework.library.common.utils.ToastUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ADA_CouponList extends RecyclerView.Adapter<ADA_CouponList.CouponViewHolder> {

    private Context mContext;
    private List<CouponEntity.ListEntity> mList;

    private String mTotalPrice;

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClicked(String id, String ticketId, String price);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public ADA_CouponList(String totalPrice) {
        mList = new ArrayList<>();
        mTotalPrice = totalPrice;
    }

    public void setList(List<CouponEntity.ListEntity> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public CouponViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        return new CouponViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_coupons_recycler,
                parent, false));
    }

    @Override
    public void onBindViewHolder(CouponViewHolder holder, int position) {
        final CouponEntity.ListEntity entity = mList.get(position);
        //优惠卷状态 0未使用  1已使用 2已过期
        if ("0".equals(entity.getStatus())) {
            if (StringUtil.isEmpty(mTotalPrice)) {
                holder.itemCouponsLlParent.setBackground(mContext.getResources().getDrawable(R.mipmap.ic_coupons_item_unused));
            } else {
                //可用
                if (Double.parseDouble(mTotalPrice) > Double.parseDouble(entity.getTicketAmount()) && Double.parseDouble(mTotalPrice) >= Double.parseDouble(entity.getLimitAmount())) {
                    holder.itemCouponsLlParent.setBackground(mContext.getResources().getDrawable(R.mipmap.ic_coupons_item_unused));
                } else {
                    //不可用
                    holder.itemCouponsLlParent.setBackground(mContext.getResources().getDrawable(R.mipmap.ic_coupons_item_used));
                }
            }

            holder.itemCouponsTvUse.setText("立即使用>>");
            holder.itemCouponsTvUse.setEnabled(true);
            holder.itemCouponsTvUse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    doOnClick(entity);
                }
            });

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    doOnClick(entity);
                }
            });
        } else if ("1".equals(entity.getStatus())) {
            holder.itemCouponsLlParent.setBackground(mContext.getResources().getDrawable(R.mipmap.ic_coupons_item_used));
            holder.itemCouponsTvUse.setText("已使用");
            holder.itemCouponsTvUse.setEnabled(false);
        } else if ("2".equals(entity.getStatus())) {
            holder.itemCouponsLlParent.setBackground(mContext.getResources().getDrawable(R.mipmap.ic_coupons_item_used));
            holder.itemCouponsTvUse.setText("已过期");
            holder.itemCouponsTvUse.setEnabled(false);
        }
        //优惠卷名字
        holder.itemCouponsTvTitle.setText(entity.getTicketName());
        //优惠卷到期时间
        holder.itemCouponsTvTime.setText(entity.getStartDate() + "至" + entity.getEndDate());
        //优惠卷价格
        String str = new BigDecimal(entity.getTicketAmount()).toPlainString();
        float scale = 0;
        // 按长度确定缩放比例
        if (str.length() > 4) {
            switch (str.length()) {
                case 5:
                    scale = 0.8f;
                    break;
                case 6:
                    scale = 0.7f;
                    break;
                case 7:
                    scale = 0.6f;
                    break;
                case 8:
                    scale = 0.5f;
                    break;
                default:
                    scale = 0.4f;
            }
        }
        holder.itemCouponsTvPrice.setText(StringSpanUtils.init().addRelativeScale(scale, "￥").addRelativeScale(scale,
                str).build());
        //副标题
        holder.itemCouponsTvDetails.setText(entity.getTicketSubName());

    }

    private void doOnClick(CouponEntity.ListEntity entity) {
        if (mOnItemClickListener != null) {
            if (entity.getStatus().equals("0")) {//待使用的优惠券
                if (Double.parseDouble(mTotalPrice) > Double.parseDouble(entity.getTicketAmount()) && Double.parseDouble(mTotalPrice) >= Double.parseDouble(entity.getLimitAmount())) {
                    mOnItemClickListener.onItemClicked(entity.getId(), entity.getTicketId(),
                            entity.getTicketAmount());
                } else {
                    if (Double.parseDouble(mTotalPrice) < Double.parseDouble(entity.getTicketAmount())) {
                        ToastUtil.makeText(mContext, "优惠金额不能大于订单金额", false);
                        return;
                    }
                    if (Double.parseDouble(mTotalPrice) < Double.parseDouble(entity.getLimitAmount())) {
                        ToastUtil.makeText(mContext, "订单金额需大于优惠券使用最低限制金额", false);
                    }
                }
            }
        } else {
            final DIALOG_CouponTip dialog = new DIALOG_CouponTip(mContext);
            dialog.show();
        }
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class CouponViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_coupons_tv_title)
        TextView itemCouponsTvTitle;
        @BindView(R.id.item_coupons_tv_details)
        TextView itemCouponsTvDetails;
        @BindView(R.id.item_coupons_tv_time)
        TextView itemCouponsTvTime;
        @BindView(R.id.item_coupons_tv_price)
        TextView itemCouponsTvPrice;
        @BindView(R.id.item_coupons_tv_use)
        TextView itemCouponsTvUse;
        @BindView(R.id.item_coupons_ll_parent)
        LinearLayout itemCouponsLlParent;

        public CouponViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
